﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace BuildHelper.Kontakt
{
    public partial class KontaktUsersForm : XtraForm
    {
        private static KontaktUsersForm _Instance;
        public static KontaktUsersForm Instance
        {
            get { return _Instance; }
        }

        private BindingList<string> binding;
        ClientNew client;
        public KontaktUsersForm()
        {
            InitializeComponent();
            binding = new BindingList<string>(ServerNew.Instance.Users);
            listUsers.DataSource = binding;
        }

        public KontaktUsersForm(ClientNew client)
            : this()
        {
            this.client = client;
            _Instance = this;
        }

        private void ContaktUser()
        {
            string str = listUsers.SelectedItem as string;
            if (str != null)
            {
                ContaktUser(str);
            }
        }

        private void ContaktUser(string nick)
        {
            client.Form.ConnectWithUser(nick);
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            _Instance = null;
        }

        private void btnMsgToAll_Click(object sender, EventArgs e)
        {
            //ContaktUser("");
        }

        private void listUsers_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            ContaktUser();
        }

        private void listUsers_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                ContaktUser();
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            if (!Program.IsExit)
            {
                e.Cancel = true;
                this.Hide();
            }
            base.OnFormClosing(e);
        }
        
        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            string str = listUsers.SelectedItem as string;
            if (str != null)
            {
                if (Admin.FormQuery.forms.ContainsKey(str))
                {
                    Admin.FormQuery.forms[str].TopMost = true;
                    Admin.FormQuery.forms[str].TopMost = false;
                }
                else
                {
                    using (Admin.FormQuery f = new Admin.FormQuery(str, str))
                    {
                        f.ShowDialog();
                    }
                }
            }
        }

        private void bbiInfoAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            using (Admin.FormAdminInfoMsg i = new Admin.FormAdminInfoMsg())
            {
                i.ValidationFunc = new Func<string, bool>(ValidateMessage);
                i.Text = "Informacja do wszystkich";
                if (i.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    ServerNew.Instance.SendToAll(Kontakt.DefaultMessages.MSG_START_INFO, i.Message);
                }
            }
        }

        private bool ValidateMessage(string msg)
        {
            if (msg.IsNull())
            {
                FormMain.Instance.ShowDxInfoDialog("Podaj wiadomość.");
                return false;
            }
            return true;
        }

        private void bbiMsgAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            using (Admin.FormAdminInfoMsg i = new Admin.FormAdminInfoMsg())
            {
                i.ValidationFunc = new Func<string, bool>(ValidateMessage);
                i.Text = "Wiadomość do wszystkich";
                if (i.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    ServerNew.Instance.SendToAll(ProgNetComponentsDx.Chat.ChatMessages.StringMsg, i.Message);
                }
            }
        }

        private void bbiInfoNewVer_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ServerNew.Instance.SendToAll(Kontakt.DefaultMessages.MSG_START_INFO, "Dostępna jest nowa wersja BuildHelper'a.");
        }

        private void bbiInfo_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            string str = listUsers.SelectedItem as string;
            if (str != null)
            {
                using (Admin.FormAdminInfoMsg f = new Admin.FormAdminInfoMsg())
                {
                    f.Text = "Informacja do " + str;
                    f.ValidationFunc = new Func<string, bool>(this.ValidateMessage);
                    if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        SendMsgToCurrent(DefaultMessages.MSG_START_INFO, f.Message);
                    }
                }
            }
        }

        private void SendMsgToCurrent(string type, string msg = "")
        {
            string str = listUsers.SelectedItem as string;
            if (str != null)
                ServerNew.Instance.SendTo(type, str, msg);
        }

        private void bbiSnapshot_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
           // SendMsgToCurrent(DefaultMessages.MSG_SNAPSHOT);
        }

        private void bbiWersja_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SendMsgToCurrent(DefaultMessages.MSG_GET_APP_VER);
        }

        private void bbiSprawdzWersjeUWszystkich_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ServerNew.Instance.SendToAll(DefaultMessages.MSG_GET_APP_VER, "");
        }

        private void bbiGetErrors_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SendMsgToCurrent(DefaultMessages.MSG_GET_LOG);
        }
    }
}

